#include "lcd_init.h"
#include "stdio.h"
#include <string.h>
// #include "app.h"
#define Command 0
#define Parameter 1
typedef void (*FunType)(void);
//**********************************write function begin********************************************
void wr_cmd_4spi_8bcs(uint8_t par)
{
    SPI_CSXLow();
    SPI_DCXLow();
    for (uint8_t i = 0; i < 8; i++)
    {
        SPI_SCLLow();
        if (par & 0x80)
        {
            SPI_SDAHigh();
        }
        else
        {
            SPI_SDALow();
        }
        SPI_SCLHigh();
        par <<= 1;
    }
    SPI_CSXHigh();
}
void wr_dat_4spi_8bcs(uint8_t par)
{
    SPI_CSXLow();
    SPI_DCXHigh();
    for (uint8_t i = 0; i < 8; i++)
    {
        SPI_SCLLow();
        if (par & 0x80)
        {
            SPI_SDAHigh();
        }
        else
        {
            SPI_SDALow();
        }
        SPI_SCLHigh();
        par <<= 1;
    }
    SPI_CSXHigh();
}

void sda_state(uint8_t state)
{
    GPIO_InitTypeDef GPIO_InitStruct = {0};
    if (state == 0)
    {
        GPIO_InitStruct.Pin = SPI_SDA_Pin;
        GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
        GPIO_InitStruct.Pull = GPIO_NOPULL;
        GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_HIGH;
        HAL_GPIO_Init(SPI_SDA_GPIO_Port, &GPIO_InitStruct);
    }
    else if (state == 1)
    {
        GPIO_InitStruct.Pin = SPI_SDA_Pin;
        GPIO_InitStruct.Mode = GPIO_MODE_INPUT;
        GPIO_InitStruct.Pull = GPIO_NOPULL;
        HAL_GPIO_Init(SPI_SDA_GPIO_Port, &GPIO_InitStruct);
    }
}

uint8_t rd_spi(uint8_t reg)
{
    sda_state(io_writ);
    uint8_t re_val = 0;
    uint8_t addr = reg;
    int i = 0;

    SPI_CSXLow();
    SPI_DCXLow();

    for (i = 0; i < 8; i++)
    {
        SPI_SCLLow();
        if (reg & 0x80)
        {
            SPI_SDAHigh();
        }
        else
        {
            SPI_SDALow();
        }
        reg = reg << 1;
        SPI_SCLHigh();
    }
    sda_state(io_read);
    for (i = 0; i < 8; i++)
    {
        SPI_SCLLow();
        re_val = re_val << 1;
        re_val = re_val + HAL_GPIO_ReadPin(SPI_SDA_GPIO_Port, SPI_SDA_Pin);
        SPI_SCLHigh();
    }
    SPI_CSXHigh();
    printf("reg %02x is %02X\n\r", addr, re_val);

    sda_state(io_writ);

    return re_val;
}

uint8_t rd_spi_04(void)
{
    sda_state(io_writ);
    uint8_t re_val1 = 0;
    uint8_t re_val2 = 0;
    uint8_t re_val3 = 0;
    uint8_t reg = 0x04;
    uint8_t state = 0;
    int i = 0;

    SPI_CSXLow();
    SPI_DCXLow();

    for (i = 0; i < 8; i++)
    {
        SPI_SCLLow();
        if (reg & 0x80)
        {
            SPI_SDAHigh();
        }
        else
        {
            SPI_SDALow();
        }
        reg = reg << 1;
        SPI_SCLHigh();
    }
    sda_state(io_read);

    SPI_SCLLow();
    SPI_SCLHigh();
    for (i = 0; i < 8; i++)
    {
        SPI_SCLLow();
        re_val1 = re_val1 << 1;
        re_val1 = re_val1 + HAL_GPIO_ReadPin(SPI_SDA_GPIO_Port, SPI_SDA_Pin);
        SPI_SCLHigh();
    }
    for (i = 0; i < 8; i++)
    {
        SPI_SCLLow();
        re_val2 = re_val2 << 1;
        re_val2 = re_val2 + HAL_GPIO_ReadPin(SPI_SDA_GPIO_Port, SPI_SDA_Pin);
        SPI_SCLHigh();
    }
    for (i = 0; i < 8; i++)
    {
        SPI_SCLLow();
        re_val3 = re_val3 << 1;
        re_val3 = re_val3 + HAL_GPIO_ReadPin(SPI_SDA_GPIO_Port, SPI_SDA_Pin);
        SPI_SCLHigh();
    }
    SPI_CSXHigh();
    printf("reg id is %02X %02X %02X\n\r", re_val1, re_val2, re_val3);

    sda_state(io_writ);

    return state;
}

void wr_cmd_parall1_8(uint16_t par)
{
    PAR_DCXLow();
    PAR_CSLow();
    GPIOB->ODR = par;

    PAR_WRLow();
    PAR_WRHigh();
    PAR_CSHigh();
}
void wr_dat_parall1_8(uint16_t par)
{
    PAR_DCXHigh();
    PAR_CSLow();
    GPIOB->ODR = par;

    PAR_WRLow();
    PAR_WRHigh();
    PAR_CSHigh();
}
void wr_cmd_parall1_16(uint16_t par)
{
    PAR_DCXLow();
    SPI_CSXLow();
    PAR_WRLow();
    GPIOB->ODR = par;

    PAR_WRHigh();
}
void wr_dat_parall1_16(uint16_t par)
{
    PAR_DCXHigh();
    PAR_CSLow();
    PAR_WRLow();
    GPIOB->ODR = par;

    PAR_WRHigh();
}

void wr_cmd_parall2_8(uint16_t par)
{
    PAR_DCXLow();
    SPI_CSXLow();
    PAR_WRLow();
    GPIOB->ODR = par << 1;

    PAR_WRHigh();
}
void wr_dat_parall2_8(uint16_t par)
{
    PAR_DCXHigh();
    PAR_WRLow();
    GPIOB->ODR = par << 1;

    PAR_WRHigh();
}
//**********************************read function begin********************************************
void pb_out(void)
{
    GPIO_InitTypeDef GPIO_InitStruct = {0};

    // db 0-7
    GPIO_InitStruct.Pin = GPIO_PIN_0 | GPIO_PIN_1 | GPIO_PIN_2 | GPIO_PIN_3 |
                          GPIO_PIN_4 | GPIO_PIN_5 | GPIO_PIN_6 | GPIO_PIN_7;
    GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
    GPIO_InitStruct.Pull = GPIO_NOPULL;
    GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_HIGH;
    HAL_GPIO_Init(GPIOB, &GPIO_InitStruct);
}
void pb_in(void)
{
    GPIO_InitTypeDef GPIO_InitStruct = {0};

    // db 0-7
    GPIO_InitStruct.Pin = GPIO_PIN_0 | GPIO_PIN_1 | GPIO_PIN_2 | GPIO_PIN_3 |
                          GPIO_PIN_4 | GPIO_PIN_5 | GPIO_PIN_6 | GPIO_PIN_7;
    GPIO_InitStruct.Mode = GPIO_MODE_INPUT;
    GPIO_InitStruct.Pull = GPIO_NOPULL;
    GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_HIGH;
    HAL_GPIO_Init(GPIOB, &GPIO_InitStruct);
}
uint8_t i801_read_byte(void)
{
    uint8_t v;

    PAR_DCXHigh();
    PAR_RDLow();
    v = GPIOB->IDR;
    PAR_RDHigh();

    return v;
}
void rd_parall1_8(uint8_t c, uint8_t *d, uint32_t l)
{
    uint8_t buf[128];

    wr_cmd_parall1_8(c);
    pb_in();
    for (int i = 0; i < l; i++)
    {
        buf[i] = i801_read_byte();
    }
    PAR_CSHigh();
    pb_out();
    memcpy(d, buf, l);
}
//**********************************write display begin********************************************
void WR_1963_REG(uint16_t data)
{
    wr_cmd_parall1_8(data);
}

void WR_1963_PAR(uint16_t data)
{
    wr_dat_parall1_8(data);
}

void Write(uint16_t DT, uint16_t par)
{
    if (DT == 0)
    {
        wr_cmd_4spi_8bcs(par);
    }
    else
    {
        wr_dat_4spi_8bcs(par);
    }
}

void WR_7789_REG(uint16_t data)
{
    wr_cmd_4spi_8bcs(data);
}

void WR_7789_PAR(uint16_t data)
{
    wr_dat_4spi_8bcs(data);
}

void Delay(uint32_t ms)
{
    HAL_Delay(ms);
}

void init_ST7789V2(void)
{
    Write(Command, 0x11);

    Delay(120); // ms

    Write(Command, 0x36);
    Write(Parameter, 0x00);

    Write(Command, 0x3A);
    Write(Parameter, 0x66);

    Write(Command, 0xB0); // Set RGB Interface
    Write(Parameter, 0x11);
    Write(Parameter, 0xF4);

    Write(Command, 0xB1); // RGB Interface Control
    Write(Parameter, 0x40);
    Write(Parameter, 0x04);
    Write(Parameter, 0x14);

    Write(Command, 0xB2);
    Write(Parameter, 0x0C);
    Write(Parameter, 0x0C);
    Write(Parameter, 0x00);
    Write(Parameter, 0x33);
    Write(Parameter, 0x33);

    Write(Command, 0xB7);
    Write(Parameter, 0x65);

    Write(Command, 0xBB);
    Write(Parameter, 0x17);

    Write(Command, 0xC0);
    Write(Parameter, 0x2C);

    Write(Command, 0xC2);
    Write(Parameter, 0x01);

    Write(Command, 0xC3);
    Write(Parameter, 0x13);

    Write(Command, 0xC4);
    Write(Parameter, 0x20);

    Write(Command, 0xC6);
    Write(Parameter, 0x0F);

    Write(Command, 0xD0);
    Write(Parameter, 0xA4);
    Write(Parameter, 0xA1);

    Write(Command, 0xD6);
    Write(Parameter, 0xA1);

    Write(Command, 0xE0);
    Write(Parameter, 0xD0);
    Write(Parameter, 0x0C);
    Write(Parameter, 0x14);
    Write(Parameter, 0x0C);
    Write(Parameter, 0x09);
    Write(Parameter, 0x17);
    Write(Parameter, 0x39);
    Write(Parameter, 0x54);
    Write(Parameter, 0x4F);
    Write(Parameter, 0x19);
    Write(Parameter, 0x13);
    Write(Parameter, 0x13);
    Write(Parameter, 0x2D);
    Write(Parameter, 0x34);

    Write(Command, 0xE1);
    Write(Parameter, 0xD0);
    Write(Parameter, 0x13);
    Write(Parameter, 0x17);
    Write(Parameter, 0x0A);
    Write(Parameter, 0x0B);
    Write(Parameter, 0x05);
    Write(Parameter, 0x39);
    Write(Parameter, 0x43);
    Write(Parameter, 0x4D);
    Write(Parameter, 0x36);
    Write(Parameter, 0x11);
    Write(Parameter, 0x12);
    Write(Parameter, 0x2C);
    Write(Parameter, 0x32);

    Write(Command, 0x35);
    Write(Parameter, 0x00);

    Write(Command, 0x21);

    Write(Command, 0x29);

    Write(Command, 0x2A); // Column Address Set
    Write(Parameter, 0x00);
    Write(Parameter, 0x00); // 0
    Write(Parameter, 0x00);
    Write(Parameter, 0xEF);

    Write(Command, 0x2B); // Row Address Set
    Write(Parameter, 0x00);
    Write(Parameter, 0x00); // 0
    Write(Parameter, 0x01);
    Write(Parameter, 0x3F);

    Write(Command, 0x2C);
}

void init_ST7789V2_new(void)
{
    Delay(120); // ms

    Write(Command, 0x11);

    Delay(120); // ms

    Write(Command, 0x21);

    Write(Command, 0x36);
    Write(Parameter, 0x00);

    Write(Command, 0x3A);
    Write(Parameter, 0x66);

    Write(Command, 0xB0); // Set RGB Interface
    Write(Parameter, 0x11);
    Write(Parameter, 0xF4);

    Write(Command, 0xB1); // RGB Interface Control
    Write(Parameter, 0x40);
    Write(Parameter, 0x04);
    Write(Parameter, 0x14);

    Write(Command, 0xB2);
    Write(Parameter, 0x0C);
    Write(Parameter, 0x0C);
    Write(Parameter, 0x00);
    Write(Parameter, 0x33);
    Write(Parameter, 0x33);

    Write(Command, 0xB7);
    Write(Parameter, 0x75);

    Write(Command, 0xBB);
    Write(Parameter, 0x14);

    Write(Command, 0xC0);
    Write(Parameter, 0x2C);

    Write(Command, 0xC2);
    Write(Parameter, 0x01);

    Write(Command, 0xC3);
    Write(Parameter, 0x13);

    Write(Command, 0xC4);
    Write(Parameter, 0x20);

    Write(Command, 0xC6);
    Write(Parameter, 0x0F);

    Write(Command, 0xD0);
    Write(Parameter, 0xA4);
    Write(Parameter, 0xA1);

    Write(Command, 0xD6);
    Write(Parameter, 0xA1);

    Write(Command, 0xE0);
    Write(Parameter, 0xF0);
    Write(Parameter, 0x0B);
    Write(Parameter, 0x12);
    Write(Parameter, 0x0C);
    Write(Parameter, 0x0C);
    Write(Parameter, 0x09);
    Write(Parameter, 0x37);
    Write(Parameter, 0x44);
    Write(Parameter, 0x4D);
    Write(Parameter, 0x38);
    Write(Parameter, 0x13);
    Write(Parameter, 0x11);
    Write(Parameter, 0x2E);
    Write(Parameter, 0x34);

    Write(Command, 0xE1);
    Write(Parameter, 0xF0);
    Write(Parameter, 0x09);
    Write(Parameter, 0x0F);
    Write(Parameter, 0x08);
    Write(Parameter, 0x08);
    Write(Parameter, 0x23);
    Write(Parameter, 0x37);
    Write(Parameter, 0x43);
    Write(Parameter, 0x4C);
    Write(Parameter, 0x35);
    Write(Parameter, 0x10);
    Write(Parameter, 0x12);
    Write(Parameter, 0x2C);
    Write(Parameter, 0x32);

    Write(Command, 0x29);

    Write(Command, 0x2A); // Column Address Set
    Write(Parameter, 0x00);
    Write(Parameter, 0x00); // 0
    Write(Parameter, 0x00);
    Write(Parameter, 0xEF);

    Write(Command, 0x2B); // Row Address Set
    Write(Parameter, 0x00);
    Write(Parameter, 0x00); // 0
    Write(Parameter, 0x01);
    Write(Parameter, 0x3F);

    Write(Command, 0x2C);
}

void init_SSD1963(void)
{
    // INIT SSD1963
    WR_1963_REG(0xE2); // Set PLL with OSC = 10MHz (hardware)
    WR_1963_PAR(0x1D); // M[7:0] : Multiplier (M) of PLL��VCO = 10 MHz x ( 29 + 1 ) =300 MHz
    WR_1963_PAR(0x02); // N[4:0] : Divider (N) of PLL��PLL frequency = 300 MHz / (2 + 1) =100 MHz
    WR_1963_PAR(0x04); // C[  2] : Effectuate MN valueValidate M and N values
    HAL_Delay(20);

    WR_1963_REG(0xE0); // Start PLL command
    WR_1963_PAR(0x01); // enable PLL
    HAL_Delay(10);

    WR_1963_REG(0xE0); // Start PLL command again
    WR_1963_PAR(0x03); // now, use PLL output as system clock
    HAL_Delay(2);

    WR_1963_REG(0x01); // ����λ
    HAL_Delay(10);

    WR_1963_REG(0xE6); // ��������Ƶ��,20Mhz(320*240*3*60)
    WR_1963_PAR(0x00); // 2F 00  03
    WR_1963_PAR(0xC0); // FF D9  1E  85
    WR_1963_PAR(0xFF); // FF 16  E9  1d

    WR_1963_REG(0xB0); // set_lcd_mode
    WR_1963_PAR(0x20); // A5=1:24 bit mode
    WR_1963_PAR(0x40); // B[6:5]=10:Serial RGB mode,B[6:5]=11:Serial RGB+dummy mode

    //	WR_1963_PAR(0x1B);//RGB����
    WR_1963_REG(0xB4); // Set horizontal porch
    WR_1963_PAR(0x01); // HT_H (Total Horizontal Period = 298 → 0x012A)
    WR_1963_PAR(0x2A); // HT_L
    WR_1963_PAR(0x00); // HPS_H (Horizontal Sync Start Position = hpw + hbp = 20 → 0x0014)
    WR_1963_PAR(0x14); // HPS_L
    WR_1963_PAR(0x09); // HPW (Horizontal Pulse Width = hpw - 1 = 9 → 0x09)
    WR_1963_PAR(0x00); // LPS_H (Horizontal Back Porch = hbp = 10 → 0x000A)
    WR_1963_PAR(0x0A); // LPS_L

    WR_1963_REG(0xB6); // Set vertical period
    WR_1963_PAR(0x01); // VT_H
    WR_1963_PAR(0x4F); // VT_L		4+4+320+8-1=335(0x014F)
    WR_1963_PAR(0x00); // VPS_H
    WR_1963_PAR(0x0E); // VPS_L   4+8=12(0x0C)
    WR_1963_PAR(0x02); // VPW     4-1=3(0x05)
    WR_1963_PAR(0x00); // FPS_H
    WR_1963_PAR(0x0B); // FPS_L   4(0x08)

    WR_1963_REG(0xF0); // set_pixel_data_interface in the parallel host processor
    WR_1963_PAR(0x00); // A[2:0] : 000 8-bit,Pixel Data Interface Format

    WR_1963_REG(0xBE); // ����PWM���set_pwm_conf
    WR_1963_PAR(0x05); // PWM frequency
    WR_1963_PAR(0xfe); // PWM duty cycle
    WR_1963_PAR(0x01); // 3����C 01

    WR_1963_REG(0x29); // ������ʾ

    WR_1963_REG(0x2A); // set_column_address
    WR_1963_PAR(0x00); // Start column number
    WR_1963_PAR(0x00); //
    WR_1963_PAR(0x00); // End column number
    WR_1963_PAR(0XEF); //

    WR_1963_REG(0x2B); // set_page_address
    WR_1963_PAR(0x00); // Start page (row) number
    WR_1963_PAR(0x00); //
    WR_1963_PAR(0x01); // End page (row) number
    WR_1963_PAR(0X3F); //

    WR_1963_REG(0xB8); // set_gpio_conf
    WR_1963_PAR(0x00); // GPIO3~0 is controlled by host,GPIO3~0 is input
    WR_1963_PAR(0x00); // GPIO0 is used to control the panel power with enter_sleep_mode (0x10) or exit_sleep_mode (0x11)

    WR_1963_REG(0xba); // set_gpio_value
    WR_1963_PAR(0X01); // GPIO0 outputs 1

    WR_1963_REG(0x36); // set_address_mode
    WR_1963_PAR(0x00); // Y,X,XY,

    WR_1963_REG(0x11); // Exit Sleep
    HAL_Delay(120);
}
/**
 * @brief Initialize and configure the SSD1963 LCD controller for 60Hz frame rate
 * @retval None
 */
void init_deepseek(void)
{
    /* 1. PLL Configuration (same as before) */
    WR_1963_REG(0xE2); // Set PLL command (OSC = 10MHz)
    WR_1963_PAR(0x1D); // M = 29 → VCO = 10MHz * (29 + 1) = 300MHz
    WR_1963_PAR(0x02); // N = 2 → PLL = 300MHz / (2 + 1) = 100MHz
    WR_1963_PAR(0x04); // Validate M and N values
    HAL_Delay(20);

    /* 2. Start PLL (same as before) */
    WR_1963_REG(0xE0); // Start PLL command
    WR_1963_PAR(0x01); // Enable PLL
    HAL_Delay(10);

    /* 3. Switch to PLL clock (same as before) */
    WR_1963_REG(0xE0); // Start PLL command again
    WR_1963_PAR(0x03); // Use PLL output as system clock
    HAL_Delay(2);

    /* 4. Soft Reset (same as before) */
    WR_1963_REG(0x01); // Software reset
    HAL_Delay(10);

    /* 5. Configure Pixel Clock for ~60Hz */
    // Assuming 240x320 display with these timings:
    // Total clocks per line = 240 (active) + 10 (hpw) + 10 (hbp) + 38 (hfp) = 298
    // Total lines per frame = 320 (active) + 4 (vsw) + 4 (vbp) + 8 (vfp) = 336
    // Frame rate = PLL_CLK / (HT * VT) = 100MHz / (298 * 336) ≈ 60Hz
    WR_1963_REG(0xE6); // Set pixel clock frequency
    WR_1963_PAR(0x00); // Configuration parameters
    WR_1963_PAR(0xC0);
    WR_1963_PAR(0xFF);

    /* 6. Set LCD Mode (same as before) */
    WR_1963_REG(0xB0); // Set LCD mode command
    WR_1963_PAR(0x00); // 24-bit mode
    WR_1963_PAR(0x40); // Serial RGB mode
    WR_1963_PAR(0x00);
    WR_1963_PAR(0xEF);
    WR_1963_PAR(0x01);
    WR_1963_PAR(0x3F);
    //WR_1963_PAR(0x18);

    /* 7. Horizontal Timing Configuration */
    // hpw = 10-1 = 9 (register value is hpw-1)
    // hbp = 10
    // hfp = 38
    // Total = 240 + 10 + 10 + 38 = 298 (0x012A)
    WR_1963_REG(0xB4); // Set horizontal porch
    WR_1963_PAR(0x01); // HT_H (Total Horizontal Period = 298 → 0x012A)
    WR_1963_PAR(0x2A); // HT_L
    WR_1963_PAR(0x00); // HPS_H (Horizontal Sync Start Position = hpw + hbp = 20 → 0x0014)
    WR_1963_PAR(0x14); // HPS_L
    WR_1963_PAR(0x09); // HPW (Horizontal Pulse Width = hpw - 1 = 9 → 0x09)
    WR_1963_PAR(0x00); // LPS_H (Horizontal Back Porch = hbp = 10 → 0x000A)
    WR_1963_PAR(0x0A); // LPS_L

    /* 8. Vertical Timing Configuration */
    // vsw = 4-1 = 3 (register value is vsw-1)
    // vbp = 4
    // vfp = 8
    // Total = 320 + 4 + 4 + 8 = 336 (0x0150)
    WR_1963_REG(0xB6); // Set vertical period
    WR_1963_PAR(0x01); // VT_H (Total Vertical Period = 336 → 0x0150)
    WR_1963_PAR(0x50); // VT_L
    WR_1963_PAR(0x00); // VPS_H (Vertical Sync Start Position = vsw + vbp = 8 → 0x0008)
    WR_1963_PAR(0x08); // VPS_L
    WR_1963_PAR(0x03); // VPW (Vertical Pulse Width = vsw - 1 = 3 → 0x03)
    WR_1963_PAR(0x00); // FPS_H (Vertical Back Porch = vbp = 4 → 0x0004)
    WR_1963_PAR(0x04); // FPS_L

    /* 9. Pixel Data Interface (same as before) */
    WR_1963_REG(0xF0); // Set pixel data interface
    WR_1963_PAR(0x00); // 8-bit interface

    /* 11. Display Settings (same as before) */
    WR_1963_REG(0x29); // Display on

    /* 12. Set Column Address (same as before) */
    WR_1963_REG(0x2A); // Set column address
    WR_1963_PAR(0x00); // Start column = 0
    WR_1963_PAR(0x00);
    WR_1963_PAR(0x00); // End column = 239 (0xEF)
    WR_1963_PAR(0xEF);

    /* 13. Set Page Address (same as before) */
    WR_1963_REG(0x2B); // Set page address
    WR_1963_PAR(0x00); // Start page = 0
    WR_1963_PAR(0x00);
    WR_1963_PAR(0x01); // End page = 319 (0x13F)
    WR_1963_PAR(0x3F);

    /* 16. Set Address Mode (same as before) */
    WR_1963_REG(0x36); // Set address mode
    WR_1963_PAR(0x00); // Normal X-Y addressing

    /* 17. Exit Sleep Mode (same as before) */
    WR_1963_REG(0x11); // Exit sleep mode
    HAL_Delay(120);
}

void LCD_RESET(void)
{
    LCD_RSTHigh();
    HAL_Delay(10); // ms
    LCD_RSTLow();
    HAL_Delay(10); // ms
    LCD_RSTHigh();
    HAL_Delay(10); // ms
}

void write_cmd(uint16_t par)
{
    wr_cmd_4spi_8bcs(par);
}

void write_data(uint16_t par)
{
    wr_dat_4spi_8bcs(par);
}

void delay(uint32_t ms)
{
    HAL_Delay(ms);
}

void fillcolor(uint8_t dat_r, uint8_t dat_g, uint8_t dat_b)
{
    uint32_t i, j;

    WR_1963_REG(0x2C); // Memory write

    for (i = 0; i < 240; i++)
        for (j = 0; j < 320; j++)
        {
            WR_1963_PAR(dat_r);
            WR_1963_PAR(dat_g);
            WR_1963_PAR(dat_b);
        }
}

void lcd_main(void)
{
    LCD_RESET();
    rd_spi(0xDA);
    rd_spi(0xDB);
    rd_spi(0xDC);
    // init_ST7789V2();
    init_ST7789V2_new();
    // init_SSD1963();
    init_deepseek();
}